package com.yonyou.crm.sprc.opportunity.service;

import com.yonyou.crm.base.brand.service.BrandsBizServiceImpl;
import com.yonyou.crm.base.measure.service.MeasuresBizServiceImpl;
import com.yonyou.crm.base.product.service.PrdtypeBizServiceImpl;
import com.yonyou.crm.base.product.service.ProductBizServiceImpl;
import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.field.handler.FieldRelationHandler;
import com.yonyou.crm.common.field.relation.FieldRelation;
import com.yonyou.crm.common.field.relation.RefFieldRelation;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.sprc.opportunity.entity.OpportunityBVO;
import com.yonyou.iuap.context.InvocationInfoProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OpportunityBBizServiceImpl extends BaseBizServiceImpl<OpportunityBVO> {

    @Autowired
    private ProductBizServiceImpl productService;

    @Autowired
    private PrdtypeBizServiceImpl productTypeService;

    @Autowired
    private BrandsBizServiceImpl brandService;

    @Autowired
    private MeasuresBizServiceImpl measureService;



    protected void beforeBatchInsert(List<OpportunityBVO> opportunityBVOS) {
        Long loginUserID = new Long(InvocationInfoProxy.getParameter("id"));
        Date date = new Date();
        for (OpportunityBVO bvo : opportunityBVOS) {
            bvo.setTenantId(new Long(InvocationInfoProxy.getTenantid()));
            bvo.setSysCreatedTime(date);
            bvo.setSysCreatedUserId(loginUserID);
            bvo.setSysModifiedTime(date);
            bvo.setSysModifiedUserId(loginUserID);
            if(bvo.getProductTypeId() == null)
            bvo.setProductTypeId(new Long(0));
            if(bvo.getBrandId() == null)
            bvo.setBrandId(new Long(0));
            if(bvo.getMeasureId() == null)
            bvo.setMeasureId(new Long(0));
        }
    }

    public void batchInsert(List<OpportunityBVO> opportunityBVOS) {
        beforeBatchInsert(opportunityBVOS);
        dao.saveSql(OpportunityBVO.class, "batchInsert", opportunityBVOS);
    }

    protected void beforeBatchUpdate(List<OpportunityBVO> opportunityBVOS) {
        Long loginUserID = new Long(InvocationInfoProxy.getParameter("id"));
        Date date = new Date();
        for (OpportunityBVO bvo : opportunityBVOS) {
            bvo.setSysModifiedTime(date);
            bvo.setSysModifiedUserId(loginUserID);
        }
    }

    public void batchUpdate(List<OpportunityBVO> opportunityBVOS) {
        beforeBatchUpdate(opportunityBVOS);
        dao.updateSql(OpportunityBVO.class, "batchUpdate", opportunityBVOS);
    }

    public List<OpportunityBVO> getVOSByOpportunityID(Long id) {
        List<OpportunityBVO> bvoList = new ArrayList<OpportunityBVO>();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("opportunityId", id);
        List<Map<String, Object>> bvoListMap = dao.selectList(OpportunityBVO.class, "getVOSByOpportunityID", param);
        for(int i = 0 ; i < bvoListMap.size(); i++) {
            OpportunityBVO bvo = (OpportunityBVO)bvoListMap.get(i);
            bvoList.add(bvo);
        }
        transOpportunityBVO(bvoList);
        return bvoList;
    }

    private List<OpportunityBVO> transOpportunityBVO(List<OpportunityBVO> opportunityBVOList) {

        if (opportunityBVOList != null && opportunityBVOList.size() > 0) {

            List<FieldRelation<OpportunityBVO>> relations = new ArrayList<FieldRelation<OpportunityBVO>>();
            //参照项：产品
            relations.add(new RefFieldRelation<OpportunityBVO>(OpportunityBVO.class, "productId", new String[]{"productName"}, productService, "selectFieldsByIds"));
            //参照项：产品分类
            relations.add(new RefFieldRelation<OpportunityBVO>(OpportunityBVO.class, "productTypeId", new String[]{"productTypeName"}, productTypeService, "selectFieldsByIds"));
            //参照项：品牌
            relations.add(new RefFieldRelation<OpportunityBVO>(OpportunityBVO.class, "brandId", new String[]{"brandName"}, brandService, "selectFieldsByIds"));
            //参照项：计量单位
            relations.add(new RefFieldRelation<OpportunityBVO>(OpportunityBVO.class, "measureId", new String[]{"measureName"}, measureService, "selectFieldsByIds"));

            new FieldRelationHandler<OpportunityBVO>().handler(opportunityBVOList, relations);
        }

        return opportunityBVOList;
    }

    public Page<OpportunityBVO> getPageByOpportunityID(Page<OpportunityBVO> page, Map<String, Object> paramMap, Long opportunityID) {
        Page<OpportunityBVO> opportunityBVOPage = null;
        paramMap.put("opportunityId",opportunityID);
        if (paramMap != null) {
            opportunityBVOPage = super.pageSelect(OpportunityBVO.class, page, paramMap.keySet().toArray(new String[0]), paramMap.values().toArray(new Object[0]));
        } else {
            opportunityBVOPage = super.pageSelect(OpportunityBVO.class, page, new String[]{"opportunityId"}, new Object[]{opportunityID});
        }

        transOpportunityBVO(opportunityBVOPage.getData());

        return opportunityBVOPage;
    }

    public int getChildCounntByOpportunityID(Long id) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("opportunityId", id);
        int childCount = dao.countAll(OpportunityBVO.class, "getChildCounntByOpportunityID", param);

        return childCount;
    }
}
